//
//  serviceGetWISAddress.m
//  wAudioShare
//
//  Created by 赵帅 on 13-12-13.
//  Copyright (c) 2013年 赵帅. All rights reserved.
//

#import "serviceGetWISAddress.h"

@implementation serviceGetWISAddress
#pragma mark Initialization & Deallocation

-(id)initWithDelegate:(id <serviceGetWISAddressDelegate>) aDelegate
{
	self = [super init];
	if (self)
	{
		self.m_serviceHandler = aDelegate;
	}
    
	return self;
}



#pragma mark -
#pragma mark Configure the request
-(NSURL*)targetURL
{
    NSString *urlStr = [m_dicSendData objectForKey:@"serverurl"];
	NSURL *theUrl = [[NSURL alloc] initWithString:urlStr];
	
	return theUrl;
}

-(void)configureRequest
{
	[super configureRequest];
	[m_request setRequestMethod:@"GET"];
}

#pragma mark -
#pragma mark Parse the response
-(void)parseResponseData:(NSData *)data
{
	[super parseResponseData:data];
    
	NSMutableString * retStr = [NSMutableString stringWithString:m_request.responseString];
	
    if(retStr == nil)
    {
        [m_dicReceiveData setObject:SERVICE_FAIL forKey:@"responsestatus"];
        return;
    }
    
    NSRange hostRange = [retStr rangeOfString:@"host["];
    NSRange portRange = [retStr rangeOfString:@"]port["];
    NSRange loadRange = [retStr rangeOfString:@"]load"];
    
    if(hostRange.location == NSNotFound || portRange.location == NSNotFound || loadRange.location == NSNotFound)
    {
        [m_dicReceiveData setObject:SERVICE_FAIL forKey:@"responsestatus"];
        return;
    }
    
    NSRange range;
    range.location = hostRange.location+hostRange.length;
    range.length = portRange.location-range.location;
    
    NSString * host = [retStr substringWithRange:range];
    
    range.location = portRange.location+portRange.length;
    range.length = loadRange.location-range.location;
    
    NSString * port = [retStr substringWithRange:range];
    
    [m_dicReceiveData setObject:host forKey:@"host"];
    [m_dicReceiveData setObject:port forKey:@"port"];
}

#pragma mark -
#pragma mark Pass Data To UI
-(void)passDataOut
{
	[super passDataOut];
	
	if (nil != self.m_serviceHandler
		&& [self.m_serviceHandler respondsToSelector:@selector(serviceGetWISAddressCallBack:)])
	{
        [self.m_serviceHandler performSelectorOnMainThread:@selector(serviceGetWISAddressCallBack:) withObject:m_dicReceiveData waitUntilDone:YES];
	}
}
@end
